package com.nulp.psycho.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.nulp.psycho.db.ConnectionManager;
import com.nulp.psycho.db.dao.BaseDao;
import com.nulp.psycho.db.mapping.Converter;
import com.nulp.psycho.db.model.GroupResult;

public class GroupResultDao implements BaseDao{

	private final String ADD_GROUP_RESULT = "INSERT INTO group_result (group_id, test_id, user_id, result_id) VALUES (?,?,?,?)";
	private final String GET_GROUP_RESULT_BY_ID = "SELECT * FROM group_result WHERE id = ?";
	private final String REMOVE_GROUP_RESULT = "DELETE FROM group_result WHERE id = ?";
	private final String UPDATE_GROUP_RESULT = "UPDATE group_result SET group_id=?, test_id=?, user_id=?, result_id=? WHERE id = ?";
	
	
	@Override
	public <T> void create(T element) {
		GroupResult temp = (GroupResult) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(ADD_GROUP_RESULT)) {
			preparedStatement.setInt(1, temp.getGroupId());
			preparedStatement.setInt(2, temp.getTestId());
			preparedStatement.setInt(3, temp.getUserId());
			preparedStatement.setInt(4, temp.getResultId());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}		
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T read(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		GroupResult groupRes = null;
		try (PreparedStatement statement = connection
				.prepareStatement(GET_GROUP_RESULT_BY_ID)) {
			statement.setInt(1, id);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				groupRes = Converter.<GroupResult> fromResaultSetToObject(rs,
						GroupResult.class);
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) groupRes;	
	}

	@Override
	public void delete(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(REMOVE_GROUP_RESULT)) {
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}		
	}

	@Override
	public <T> void update(T element) {
		GroupResult temp = (GroupResult) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(UPDATE_GROUP_RESULT)) {
			preparedStatement.setInt(5, temp.getId());
			preparedStatement.setInt(1, temp.getGroupId());
			preparedStatement.setInt(2, temp.getTestId());
			preparedStatement.setInt(3, temp.getUserId());
			preparedStatement.setInt(4, temp.getResultId());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}		
	}
	
}
